package com.bwie.record.consumer;

import com.alibaba.fastjson.JSON;
import com.bwie.record.domain.DataTwo;
import com.bwie.record.service.DataTwoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

@Slf4j
@Component
public class DataConsumer2 {
    @Autowired
    private DataTwoService dataTwoService;

    @RabbitListener(queues = "dataTwoQueue")
    public void consumeDataTwo(String dataTwo) {
        DataTwo data = JSON.parseObject(dataTwo,DataTwo.class);
        log.info("消费者2接收到数据： " + data +", 时间："+ new Date().toLocaleString());

        DataTwo dataTwoInfo = dataTwoService.findById(data.getId());
        if (dataTwoInfo == null) {
            dataTwoService.addDataTwo(data);
        } else {
            dataTwoService.updateDataTwo(data);
        }
    }
}
